package pers.qianyu.month_202012.date_20201206;

import org.junit.*;

/**
 * 474. 一和零
 * https://leetcode-cn.com/problems/ones-and-zeroes/
 *
 * @author mizzle rain
 * @date 2020-12-06 13:25
 */
public class FindMaxForm {
    public int findMaxForm(String[] strs, int m, int n) {
        int[][][] dp = new int[strs.length + 1][m + 1][n + 1];
        for (int i = 1; i <= strs.length; i++) {
            int count0 = count(strs[i - 1], '0');
            int count1 = count(strs[i - 1], '1');
            for (int j = 0; j <= m; j++) {
                for (int k = 0; k <= n; k++) {
                    if (count0 <= j && count1 <= k) {
                        dp[i][j][k] = Math.max(dp[i - 1][j][k],
                                dp[i - 1][j - count0][k - count1] + 1);
                    } else {
                        dp[i][j][k] = dp[i - 1][j][k];
                    }
                }
            }
        }
        return dp[strs.length][m][n];
    }

    private int count(String s, char c) {
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == c) {
                res++;
            }
        }
        return res;
    }


    @Test
    public void test1() {
        String[] strs = {"10", "0001", "111001", "1", "0"};
        int m = 5, n = 3;
        int result = new FindMaxForm().findMaxForm(strs, m, n);
        System.out.println("result = " + result);
    }

    @Test
    public void test2() {
        String[] strs = {"10", "0", "1"};
        int m = 1, n = 1;
        int result = new FindMaxForm().findMaxForm(strs, m, n);
        System.out.println("result = " + result);
    }

    @Test
    public void test3() {
        String[] strs = {"111", "1000", "1000", "1000"};
        int m = 9, n = 3;
        int result = new FindMaxForm().findMaxForm(strs, m, n);
        System.out.println("result = " + result);
    }
}
